package org.zerg.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zerg.dao.ImageDao;
import org.zerg.dao.ProductDao;
import org.zerg.dto.FileDto;
import org.zerg.dto.Page;
import org.zerg.entity.Image;
import org.zerg.entity.Product;
import org.zerg.entity.ProductImage;
import org.zerg.entity.ProductNote;
import org.zerg.entity.ProductProperty;
import org.zerg.service.ProductService;

@Service
public class ProductServiceImpl extends BaseServiceImpl implements ProductService{

	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private ImageDao imageDao;
	
	@Value("${adImage.url}")
	private String adImageUrl;
	
	public List<Product> getProductByCa(int id) {
		List<Product> list = productDao.getProductByCa(id);
		for (Product product : list) {
			product.setMainImgUrl(adImageUrl+product.getMainImgUrl());
		}
		return list;
	}

	public Product getProductDelit(int id) {
		Product product = productDao.getProductById(id);
		List<ProductImage> imgs = productDao.getProductImg(id);
		List<ProductProperty> productProperty = productDao.getProductPro(id);
		List<ProductNote> note = productDao.getProductNote(id);
		for (ProductImage img : imgs) {
			img.getImgUrl().setUrl(adImageUrl+img.getImgUrl().getUrl());
		}
		product.setMainImgUrl(adImageUrl+product.getMainImgUrl());
		product.setProperties(productProperty);
		product.setImgs(imgs);
		product.setNote(note);
		return product;
	}

	public List<Product> getRecentProduct() {
		List<Product> list = productDao.getRecentProduct(15);
		for (Product product : list) {
			product.setMainImgUrl(adImageUrl+product.getMainImgUrl());
		}
		return list;
	}

	public Product inserPoduct(Product product, FileDto dto) {
		String fileName = this.saveFile(dto);
		imageDao.addImage("/"+fileName);
		Image image = imageDao.getImage("/"+fileName);
		product.setImgId(image.getId());
		product.setMainImgUrl("/"+fileName);
		productDao.inserProduct(product);
		Product p = productDao.getProductByName(product.getName());
		return p;
	}

	public List<Product> getProductByPage(int page) {
		//�����Ʒ����
		int total = productDao.getProducrTotal();
		if((page-1)*15>total){
			return null;
		}
		List<Product> list = productDao.getProductPage((page-1)*15);
		for (Product product : list) {
			product.setMainImgUrl(adImageUrl+product.getMainImgUrl());
		}
		return list;
	}
	
	public Page pageMessage(int page){
		int total = productDao.getProducrTotal();
		double to = total;
		int totalPage = (int) Math.ceil(to/15);
		Page pa = new Page(page,totalPage,total,15);
		return pa;
	}

	public void deleteProduct(int id) {
		productDao.deleteProduct(id);
	}

	public void modifyProductNoFile(Product product) {
		productDao.modifyProductNoFile(product);
	}

	public void modifyProduct(Product product, FileDto dto) {
		String fileName = this.saveFile(dto);
		imageDao.addImage("/"+fileName);
		Image image =imageDao.getImage("/"+fileName);
		product.setMainImgUrl(image.getUrl());
		product.setImgId(image.getId());
		productDao.modifyProduct(product);
	}

	public void inserImgs(FileDto dto, int id, int order) {
		String fileName = this.saveFile(dto);
		imageDao.addImage("/"+fileName);
		Image image = imageDao.getImage("/"+fileName);
		ProductImage pro = new ProductImage();
		pro.setImgId(image.getId());
		pro.setOrder(order);
		pro.setProductId(id);
		productDao.inserProductImag(pro);
	}

	public void deleteImgs(int iid) {
		productDao.deleteProductImag(iid);
	}

	public void modifyImgs(FileDto dto, int id, int order, int classId) {
		String fileName = this.saveFile(dto);
		imageDao.addImage("/"+fileName);
		Image image = imageDao.getImage("/"+fileName);
		ProductImage pro = new ProductImage();
		pro.setImgId(image.getId());
		pro.setOrder(order);
		pro.setProductId(classId);
		pro.setId(id);
		productDao.modifyProductImag(pro);
	}

	public void modifyImgsNoFile(int id, int order, int classId) {
		ProductImage pro = new ProductImage();
		pro.setOrder(order);
		pro.setProductId(classId);
		pro.setId(id);
		productDao.modifyProductImag(pro);
	}

	public void inserProper(String name, String detali,int id) {
		ProductProperty productProperty =  new ProductProperty();
		productProperty.setName(name);
		productProperty.setDetail(detali);
		productProperty.setProductId(id);
		productDao.inserProductProper(productProperty);
	}

	public void modifyProper(int id,String name, String detali, int classId) {
		ProductProperty productProperty =  new ProductProperty();
		productProperty.setId(id);
		productProperty.setName(name);
		productProperty.setDetail(detali);
		productProperty.setProductId(classId);
		productDao.modifyProductProper(productProperty);
	}

	public void deleteProper(int iid) {
		productDao.deleteProductProper(iid);
	}

	public void inserNote(ProductNote note) {
		productDao.inserProductNote(note);
	}

	public void modifyNote(ProductNote note) {
		productDao.modifyProductNote(note);
	}

	public void deleteNote(int id) {
		productDao.deleteProductNote(id);
	}

	@Override
	public boolean isName(String name) {
		boolean result = true;
		Product product = productDao.getProductByName(name);
		if(product == null){
			result = false;
		}
		return result;
	}


}
